{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 零 前言\n",
    "- 环境：win10、pycharm、python3.8、（jupyter notebook）\n",
    "- -2021年10月18首发\n",
    "- 千山踏飞雪，秋水共长天"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一 python3 基础题目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 题目：斐波那契数列\n",
    "> 斐波那契数列（Fibonacci sequence），又称黄金分割数列，指的是这样一个数列：0、1、1、2、3、5、8、13、21、34、……。\n",
    "> 在数学上，费波那契数列是以递归的方法来定义\n",
    "F0 = 0     (n=0)\n",
    "F1 = 1    (n=1)\n",
    "Fn = F[n-1]+ F[n-2](n=>2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用递归方法\n",
    "# 递归两要素：1、递归终止条件；2、递归函数\n",
    "def fibonacci(n):\n",
    "\tif n == 1 or n == 0:\n",
    "\t\treturn n\n",
    "\telse:\n",
    "\t\treturn fibonacci(n - 1) + fibonacci(n - 2)\n",
    "fibonacci(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 题目：(斐波那契数列) 兔生兔\n",
    "(斐波那契数列)有一对兔子，从出生后第3个月起每个月都生一对兔子，\n",
    "小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问第16个月的兔子总数为多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fibonacci(n):\n",
    "\tif n == 1 or n == 0:\n",
    "\t\treturn n\n",
    "\telse:\n",
    "\t\treturn fibonacci(n - 1) + fibonacci(n - 2)\n",
    "m = 16\n",
    "total_rabbit = 0\n",
    "for i in range(1, m + 1):\n",
    "\ttotal_rabbit += fibonacci(m)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'第16月的兔子总数为15792'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"第%d月的兔子总数为%d\"%(m, total_rabbit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 题目：输出 9*9 乘法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1 \n",
      "2*1=2 2*2=4 \n",
      "3*1=3 3*2=6 3*3=9 \n",
      "4*1=4 4*2=8 4*3=12 4*4=16 \n",
      "5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n"
     ]
    }
   ],
   "source": [
    "# 分行与列考虑，共9行9列，i控制行，j控制列。\n",
    "for i in range(1, 10):\n",
    "\tfor j in range(1, i + 1):\n",
    "\t\tprint(\"%d*%d=%d\" % (i, j, i*j), end=\" \" )\n",
    "\tprint()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 题目：格式化当前时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-10-20 17:02:10\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "time_format = \"%Y-%m-%d %H:%M:%S\"\n",
    "print (time.strftime(time_format,time.localtime(time.time())))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 题目： 排列组合\n",
    "有四个数字：1、2、3、4，能组成多少个互不相同且无重复数字的三位数？各是多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "124\n",
      "132\n",
      "134\n",
      "142\n",
      "143\n",
      "213\n",
      "214\n",
      "231\n",
      "234\n",
      "241\n",
      "243\n",
      "312\n",
      "314\n",
      "321\n",
      "324\n",
      "341\n",
      "342\n",
      "412\n",
      "413\n",
      "421\n",
      "423\n",
      "431\n",
      "432\n"
     ]
    }
   ],
   "source": [
    "#range(1,5) = [1, 2, 3, 4]\n",
    "# 暴力版\n",
    "# 排列组合直观思路： 第一次 4取一； 第二次3取一，第三次2取1\n",
    "for num in range(1,5):\n",
    "\tfor num2 in range(1,5):\n",
    "\t\tif\tnum == num2:\n",
    "\t\t\tcontinue\n",
    "\t\tfor num3 in range(1,5):\n",
    "\t\t\tif num3 == num2 or num3 == num:\n",
    "\t\t\t\tcontinue\n",
    "\t\t\tprint(str(num) + str(num2) + str(num3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "扩展版（待补充）\n",
    " 如果是1到9 9个数字组成7位数呢？难道要写7层循环，显然不是很明智"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-7-5dc4927b7f02>, line 8)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-7-5dc4927b7f02>\"\u001b[1;36m, line \u001b[1;32m8\u001b[0m\n\u001b[1;33m    for j in range(a, b + 1):\u001b[0m\n\u001b[1;37m                             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "total = 7\n",
    "def joinNums(nums):\n",
    "\tstrs = [str(item) for item in nums]\n",
    "\treturn \"\".join(strs)\n",
    "a = 1\n",
    "b = 4\n",
    "total = 3\n",
    "for j in range(a, b + 1):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python工具包itertools "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "124\n",
      "132\n",
      "134\n",
      "142\n",
      "143\n",
      "213\n",
      "214\n",
      "231\n",
      "234\n",
      "241\n",
      "243\n",
      "312\n",
      "314\n",
      "321\n",
      "324\n",
      "341\n",
      "342\n",
      "412\n",
      "413\n",
      "421\n",
      "423\n",
      "431\n",
      "432\n"
     ]
    }
   ],
   "source": [
    "from itertools import permutations # 自带的排列工具包\n",
    "\n",
    "def joinTupleNums(nums):\n",
    "\t'''\n",
    "\t将元组数据 格式化输出\n",
    "\t:param nums: (1, 2, 3)\n",
    "\t:type nums: 元组\n",
    "\t:return: 123\n",
    "\t:rtype: str\n",
    "\t'''\n",
    "\tstrs = [str(item) for item in nums]\n",
    "\treturn \"\".join(strs)\n",
    "for item in permutations(range(1, 5), 3):\n",
    "\tprint(joinTupleNums(item))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5 题目：某年某月某日，日期换算\n",
    "### 5.1 输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入年份（如2021）2021\n",
      "请输入月份（如：1）10\n",
      "请输入日期（如：1）20\n"
     ]
    }
   ],
   "source": [
    "# 2021年10月15日\n",
    "\n",
    "months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 平年12月\n",
    "weeks = [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"]\n",
    "weeks_zh = [\"星期日\",\"星期一\",\"星期二\",\"星期三\",\"星期四\",\"星期五\",\"星期六\"]\n",
    "\n",
    "def isLeapYear(year):\n",
    "\t'''\n",
    "\n",
    "\t:param year: 判断year是否为闰年\n",
    "\t:type year: int\n",
    "\t:return: 是闰年返回true\n",
    "\t:rtype: bool\n",
    "\t'''\n",
    "\tif (year % 4 == 0 and year % 100 !=0) or year % 400 == 0:\n",
    "\t\treturn True\n",
    "\treturn False\n",
    "# 第一组测试数据\n",
    "# year = 2021\n",
    "# month = 9\n",
    "# day = 1\n",
    "# 第二组测试数据\n",
    "# year = 2021\n",
    "# month = 10\n",
    "# day = 15\n",
    "year = int(input(\"请输入年份（如2021）\"))\n",
    "month = int(input(\"请输入月份（如：1）\"))\n",
    "day = int(input(\"请输入日期（如：1）\"))\n",
    "total = day\n",
    "for i in range(0, month):\n",
    "\ttotal += months[i]\n",
    "# total += sum(months[:month - 1])\n",
    "if isLeapYear(year):\n",
    "\ttotal += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021年10月20日是2021的第324天'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"{year}年{month}月{day}日是{year}的第{total}天\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2 题目：输入某年某月某日，判断这一天是星期几？\n",
    "(公元1年1月1日是星期一, 基姆拉尔森计算公式)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入年份（如2021）2021\n",
      "请输入月份（如：1）10\n",
      "请输入日期（如：1）20\n"
     ]
    }
   ],
   "source": [
    "# 第一组测试数据\n",
    "# year = 2021\n",
    "# month = 9\n",
    "# day = 1\n",
    "# 第二组测试数据\n",
    "# year = 2021\n",
    "# month = 10\n",
    "# day = 15\n",
    "year = int(input(\"请输入年份（如2021）\"))\n",
    "month = int(input(\"请输入月份（如：1）\"))\n",
    "day = int(input(\"请输入日期（如：1）\"))\n",
    "week=(day+2*month+int(3*(month+1)/5)+year+int(year/4)-int(year/100)+int(year/400)+1)%7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021年10月20日是2021的第星期三天'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{}年{}月{}日是{}的第{}天\".format(year,month,day,year, weeks_zh[week])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
